<?php

class Step extends CActiveRecord
{
    CONST MEMO_MAX_LEN = 100;

    public static $autoLoadAttributes=array('id','recipeId','userId');
    
    private $_image;
    private $_saveAttributes;
    

    /********************************************************************
    *  Below are class functions
    *  WARNING: You should only use these functions to manipulate 
    *           the object instead of AR functions.
    *********************************************************************/
    
    public static function loadObjectById($id,$attributes='*')
    {
        if(is_array($attributes)){
            $attributes=array_unique(array_merge($attributes,self::$autoLoadAttributes));
        }
        return self::loadObject(array(
            'condition'=>'id=:id',
            'params'=>array(':id'=>$id),
            'select'=>$attributes,
        ));
    }
    
    public static function loadObjectsByRecipeId($id,$attributes='*')
    {
        if(is_array($attributes)){
            $attributes=array_unique(array_merge($attributes,self::$autoLoadAttributes));
        }
        return self::loadObjects(array(
            'condition'=>'recipeId=:id', 'params'=>array(':id'=>$id), 'select'=>$attributes
        ));
    } 

    public static function loadObject($criteria=array())
    {
        $criteria['limit']=1;                                 
        return array_shift(self::loadObjects($criteria));
    }
    
    public static function loadObjects($criteria=array())
    {
        return self::model()->findAll($criteria);
    }
    
    public static function saveObject(&$object, $attributes=null)
    {        
        if(is_array($attributes))
            $object->_saveAttributes=array_flip($attributes);
        else
            $object->_saveAttributes=$attributes;

        $ret=$object->save(true, $attributes);

        unset($object->_saveAttributes);
        return $ret;
    }
    
    public static function deleteObjectsByRecipeId($recipeId)
    {
        return self::model()->deleteAll('recipeId=:recipeId', array(':recipeId'=>$recipeId));
    }

    public static function deleteObject(&$object)
    {
        return $object->delete();
    }
            

    /********************************************************************
    *  Below are object & AR required functions
    *********************************************************************/

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'Step';
    }

    public function rules()
    {
        return array(
            array('recipeId,userId', 'required'),
            array('memo', 'WStringValidator', 'max'=>self::MEMO_MAX_LEN),    
        );
    }

    public function attributeLabels()
    {
        return array(
        );
    }

    protected function isSaveAttribute($name)
    {
        if(is_array($this->_saveAttributes))
            return array_key_exists($name,$this->_saveAttributes);
        return true;
    }

    protected function beforeSave()
    {
        if($this->isSaveAttribute('image')){
            $this->_image = $this->image;
            $this->image = json_encode($this->image);
        }
        return true;
    }

    protected function afterSave()
    {
        if($this->isSaveAttribute('image')){
            $this->image = $this->_image;
            $this->_image = null;
        }
    }

    protected function afterFind()
    {
        if(is_string($this->image)){
            $this->image = json_decode($this->image,true);
        }
    }
}

?>